/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PorcentajeIGV.java
 *
 * Created on 20/02/2011, 10:34:04 AM
 */

package Sistema;
import Control.Conexion;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
/**
 *
 * @author GAMA
 */
public class PorcentajeIGV extends javax.swing.JFrame {
Integer codigo_usuario_auditoria;
    /** Creates new form PorcentajeIGV */
    public PorcentajeIGV(Integer codigo_usuario_auditoria) {
        initComponents();
        this.codigo_usuario_auditoria=codigo_usuario_auditoria;
        cargar_valor_txtPorcentajeIGV();

    }
    public void cargar_valor_txtPorcentajeIGV(){
        Conexion c=new Conexion();
        String igv=c.retorna_porcentaje_igv();
        txtPorcentajeIGV.setText(""+igv);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPorcentajeIGV = new javax.swing.JTextField();

        setTitle("Porcentaje IGV");
        setIconImage(new ImageIcon(this.getClass().getResource("/imagenes/icono.gif")).getImage());
        setResizable(false);

        jPanel1.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Porcentaje para el cálculo del IGV en comprobantes");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(50, 40, 300, 14);

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Guardar16.gif"))); // NOI18N
        btnGuardar.setMnemonic('g');
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });
        jPanel1.add(btnGuardar);
        btnGuardar.setBounds(80, 130, 110, 25);

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Cancelar16.gif"))); // NOI18N
        btnCancelar.setMnemonic('c');
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel1.add(btnCancelar);
        btnCancelar.setBounds(220, 130, 100, 25);

        jLabel2.setText("Impuesto General a las Ventas (I.G.V.) :");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(50, 80, 200, 14);

        jLabel3.setText("%");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(310, 80, 20, 14);

        txtPorcentajeIGV.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtPorcentajeIGVCaretUpdate(evt);
            }
        });
        jPanel1.add(txtPorcentajeIGV);
        txtPorcentajeIGV.setBounds(270, 80, 30, 20);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-408)/2, (screenSize.height-236)/2, 408, 236);
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        guardar_igv();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
       this.setVisible(false);
}//GEN-LAST:event_btnCancelarActionPerformed

    private void txtPorcentajeIGVCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtPorcentajeIGVCaretUpdate
        // TODO add your handling code here:
        String texto=this.txtPorcentajeIGV.getText();
          char [] elemento=texto.toCharArray();
          boolean valido=true;

          for(int i=0;i<elemento.length;i++){
              if(Character.isLetter(elemento[i])){
                  valido=false;
              }
              if(elemento[i]=='-'){
                  valido=false;
              }
          }

          try{
            Float.parseFloat(texto);
          }catch(Exception e){
             valido=false;
          }
          if(!valido){
              txtPorcentajeIGV.setForeground(Color.RED);
          }else{
              txtPorcentajeIGV.setForeground(Color.BLACK);
          }
    }//GEN-LAST:event_txtPorcentajeIGVCaretUpdate
public void guardar_igv(){
    if(txtPorcentajeIGV.getForeground()==Color.RED){
         JOptionPane.showMessageDialog(this,"Formato de datos erróneo","Error",JOptionPane.ERROR_MESSAGE);
    }else{
       Conexion c=new Conexion();
       c.insertar_porcentaje_igv(codigo_usuario_auditoria, txtPorcentajeIGV.getText() );
        JOptionPane.showMessageDialog(this,"Se ha registrado con éxito","Información",JOptionPane.INFORMATION_MESSAGE);
        this.setVisible(false);
    }
}
    /**
    * @param args the command line arguments
    */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtPorcentajeIGV;
    // End of variables declaration//GEN-END:variables

}
